package com.gitee.ywj1352.算法.week02;

import java.util.Arrays;
import java.util.PriorityQueue;

public class 最小的k个数 {


    public static void main(String[] args) {
        int[] arr = {1,2,3,5,6,0};
        System.out.println(Arrays.toString(getLeastNumbers(arr,3)));

    }


    /**
     * top k 问题用 heap 做
     * @param arr
     * @param k
     * @return
     */
    public static int[] getLeastNumbers(int[] arr, int k) {
        PriorityQueue<Integer> heap = new PriorityQueue<>();
        for (int i = 0; i < arr.length; i++) {
            heap.add(arr[i]);
        }
        int[] res =   new int[k];
        for (int i = 0; i < k; i++) {

            res[i] = heap.poll();
        }
        return res;
    }
}
